import { UserService } from './user.service';
import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Post,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBody,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { GetUserListDto } from './dto/getUserList.dto';
import { UserListResDto } from './dto/userListRes.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('user')
@ApiTags('user')
@ApiBearerAuth()
export class UserController {
  constructor(private readonly userService: UserService) {}
  @UseGuards(AuthGuard('jwt'))
  @Post('list')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: '获取用户列表', description: '获取用户列表' })
  @ApiBody({ type: GetUserListDto, description: '电话,日期区间为可选' })
  @ApiResponse({
    status: 200,
    type: UserListResDto,
  })
  async getUser(@Body() reqData: GetUserListDto) {
    const resData: any = await this.userService.getUser(reqData);
    resData.list = resData.list.map((user) => ({
      ...user.toJSON(),
      phone: user.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'),
    }));
    return {
      data: resData,
    };
  }
}
